package network_programming.np1_socket_and_serversocket;

import java.io.IOException;
import java.net.Socket;

/**
 * 端口扫描器
 * 扫描主机上的端口是否已被服务器程序监听
 * */

public class PortScanner {
    public void scan(String host) {
        Socket socket = null;
        for(int port = 1; port <= 1024; port++) {
            try {
                socket = new Socket(host, port);
                System.out.println("There is a server on port " + port);
            } catch (IOException e) {
//                e.printStackTrace();
            } finally {
                if(socket != null) {
                    try {
                        socket.close();
                    } catch (IOException e) {
//                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        new PortScanner().scan("127.0.0.1");
    }
}
